﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.Enum;

namespace CourseSystem.Stat
{
    public class UrlStatFilter : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (UserHelper.LoginUser == null || UserHelper.LoginUser.UserType == UserTypeEnum.Guess)
                return;

            var ctrl = filterContext.RouteData.Values["Controller"].ToString().ToLower();
            var action = filterContext.RouteData.Values["Action"].ToString().ToLower();
            //保鲜
            if (ctrl == "stat" && action == "flash")
            {
                if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
                {
                    UrlStat.RemainStuFlash(UserHelper.LoginUser.Id);
                    return;
                }
                if (UserHelper.LoginUser.UserType == UserTypeEnum.Teacher)
                {
                    UrlStat.RemainTeaFlash(UserHelper.LoginUser.Id);
                    return;
                }
            }
            var url = ctrl + "/" + action + HttpContext.Current.Request.Url.Query.ToLower();
            if (UserHelper.LoginUser.UserType == UserTypeEnum.Student)
            {
                if (UrlMapping.StudentStatUrl.ContainsKey(url))
                {
                    UrlStat.UpdateStuUrlStat(UserHelper.LoginUser.Id, url);
                }
                return;
            }

        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            return;
        }
    }
}
